function [converge, X, iterations, U] = ZeidelMethod( A, b, eps, max )

B = zeros(size(A,1), size(A, 2));
c = b;
for i = 1 : size(A, 1)
    c(i) = c(i) / A(i,i);
    for j = 1 : size(A, 2)
       if (i == j)
           B(i, j) = 0;
       else
           B(i, j) = - A(i, j) / A(i, i);           
       end;
    end;
end;

useMax = false;
norm_no = 1;
if (norm(B, norm_no) > 1)
    norm_no = Inf;
    if (norm(B, norm_no) > 1)
        useMax = true;
    end;  
end;

n = norm(B, norm_no);

x = c;
k = 0;
while (true)
    for i = 1 : size(A, 1)
        var = 0;
        for j = 1 : size(A, 2)
            if (j ~= i)
                var = var + B(i, j) * x(j);
            end;
        end;
        x(i) = (c(i) + var) ; 
    end;
    k = k + 1;
    if (norm(A*x - b, norm_no) <= eps)
        break;
    end; 
    if (useMax && k >= max)
        break;
    end;
end;

X = x;
iterations = k;
if (useMax && k == max)
    converge = false;
    U = Inf;
else
    converge = true;
    U = A*X - b;
end;
end

